package bookCircle;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Comment {
    /** 罗登明
     * 功能：用户添加评论
     * 让用户输入他的评论并保存到comment文件里
     */
    public void addComment() {
        Scanner scanner = new Scanner(System.in);
        String file_Path ="File\\CommentFile.txt";
        System.out.println("--------发表动态--------");
        System.out.println("输入你要发表的动态:");
        String myComments = scanner.next();
        //将发表的动态写入文件
        try (Writer writer = new FileWriter(file_Path,true);
             BufferedWriter bufferedWriter = new BufferedWriter(writer))
        {
            bufferedWriter.write(myComments);
            bufferedWriter.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 功能：展示动态
     * 调用comment文件，打印出动态信息
     */
    public void displayComment() {
        File file = new File("File\\CommentFile.txt");
        ArrayList<String> lastTenLine= new ArrayList<>();//数组
        try ( BufferedReader reader = new BufferedReader(new FileReader(file));){
            String line;
            while ((line = reader.readLine()) != null) {
                lastTenLine.add(line);
                //如果动态大于10条则删除第10条之后的动态
                if (lastTenLine.size() > 10) {
                    lastTenLine.remove(0);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("----------动态展示----------");
        //倒序输出动态
        for(int i=lastTenLine.size()-1;i>=0;i--)
        {
            System.out.println((lastTenLine.size()-i)+":"+lastTenLine.get(i));
        }
        System.out.println("----------动态展示----------");
    }
}
